package com.lfz.quality_effect.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lfz.quality_effect.exception.MyException;
import com.lfz.quality_effect.model.dto.TotalScoreDTO;
import com.lfz.quality_effect.model.entity.TTotalScore;
import com.lfz.quality_effect.mapper.TTotalScoreMapper;
import com.lfz.quality_effect.service.TTotalScoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lfz.quality_effect.utils.ReturnCode;
import com.lfz.quality_effect.utils.TimeUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;

/**
 * <p>
 * 质效指标总分 服务实现类
 * </p>
 *
 * @author Destiny
 * @since 2023-03-21
 */
@Service
public class TTotalScoreServiceImpl extends ServiceImpl<TTotalScoreMapper, TTotalScore> implements TTotalScoreService {

    /**
     * 格式化
     */
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
    @Override
    public void uploadIndexTotalScore(TotalScoreDTO totalScoreDTO) {
        String court = totalScoreDTO.getCourt();
        String dataTime = totalScoreDTO.getDataTime();

        BigDecimal todayScore =  totalScoreDTO.getTodayScore();
        BigDecimal yesterdatScore = totalScoreDTO.getYesterdayScore();
        BigDecimal lastWeekScore = totalScoreDTO.getLastWeekScore();
        //日期校验
        try {
            dataTime = simpleDateFormat.format(TimeUtils.vertifyTime(dataTime));

        }catch (Exception e){
            throw new MyException(ReturnCode.RC10002.getCode(),ReturnCode.RC10002.getMsg());
        }
        //查询是否已经上传过，过滤掉已上传的
        TTotalScore totalScore =  this.getOne(new LambdaQueryWrapper<TTotalScore>()
                .eq(TTotalScore::getDataTime,dataTime)
                .eq(TTotalScore::getCourt,court));
        if (totalScore == null){
            totalScore = new TTotalScore();
            totalScore.setTodayScore(todayScore);
            totalScore.setYesterdayScore(yesterdatScore);
            totalScore.setLastWeekScore(lastWeekScore);
            totalScore.setCreateTime(LocalDateTime.now());
            totalScore.setCourt(court);
            totalScore.setDataTime(dataTime);
            this.save(totalScore);

        }
    }
}
